// pages/customerDetailInfo/customerDetailInfo.js
import api from "../../common/api.js";
import util from "../../utils/util"
import { formatTime } from "../../utils/utilsNew.js";
import p from "../../utils/platform.js";

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: "",
    unionid: "",
    tabList: ["客户分析", "客户轨迹"],
    defaultTabIndex: 0,
    analyzeTitleList: [
      "找房天数",
      "最近找房",
      "浏览我的房源",
      "浏览我的次数",
      "浏览他人房源",
      "浏览他人次数"
    ],
    analyzeName: ["loginDays", "lastTime", "setNumber", "viewNumber", "setOtherNumber", "viewOtherNumber"],
    analyzeUnitNameList: ["天", "", "套", "次", "套", "次"],
    analyzeValue: [],
    isShowCopyTabNav: false,
    content: {}, //客户基本信息
    analyzeContent: {}, //客户分析信息
    rows: [], //感兴趣房源
    trackList: [], //轨迹数据
    trackPage: 0, //轨迹当前页数
    trackPageMax: 0, //轨迹最大页数
    pageSize: 10, //单页数据量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("options===", options);

    let userId = options.userId
    let unionid = options.unionid

    this.setData({
      userId,
      unionid
    })

    api.getCustomerInfo({
      userId: this.data.userId

    }, (res) => {
      let content = res.data.content
      content.headimgurl = util.ImagesUrl + content.headimgurl
      content.unionid = unionid
        // content.mobile = 13665205105
      this.setData({
        content: res.data.content
      })
    })

    api.getCustomerData({
      unionid: this.data.unionid
    }, (res) => {
      let content = res.data.content[0] || {
        "loginDays": 1,
        "lastTime": "今日",
        "setNumber": 1,
        "viewNumber": 1,
        "setOtherNumber": 0,
        "viewOtherNumber": 0,
      }
      let analyzeContent = res.data.content[0] || {}
      let analyzeValue = this.data.analyzeName.map((item, index) => {
        return (content[item] || 0) + this.data.analyzeUnitNameList[index]
      })

      this.setData({
        analyzeValue,
        analyzeContent,
      })
    })

    api.getCustomerInterested({
      unionid: this.data.unionid
    }, (res) => {
      res.data.content.rows.forEach((item) => {
        item.isOtherPeopleHose = item.agentId !== app.globalData.sessionUser.id
        item.createTime = !item.isOtherPeopleHose ? "你的房源" : "他人房源"
        item.fcount = `浏览${(item.datePart>10000||item.app!='shulangzh')?formatTime(item.datePart):'访问时长少于10s'}/${item.fcount}次`
      })

      this.setData({
        rows: res.data.content.rows
      })
    })
  },

  tabnavIndexChange(e) {
    let index = e.detail.index
    this.setData({
      defaultTabIndex: index
    })

    if (index == 1 && this.data.trackList.length == 0) {
      this.loadTrackData()
    }
  },

  onPageScroll(e) {
    let bool = e.scrollTop >= 160

    if (this.data.isShowCopyTabNav != bool) {
      this.setData({
        isShowCopyTabNav: bool
      })
    }
  },

  loadTrackData() {
    // console.log("加载", this.data.trackPage + 1, this.data.trackPageMax);

    api.getCustomerVisitTrack({
      page: this.data.trackPage + 1,
      pageSize: this.data.pageSize,
      unionid: this.data.unionid,
      agentid: app.globalData.sessionUser.id
    }, (res) => {
      let content = res.data.content
      this.data.trackPage = content.page
      this.data.trackPageMax = content.total

      content.rows.forEach((item) => {
        item.fcount = `第${item.fcount}次`
        item.createTime = item.createTime.slice(0, item.createTime.length - 3)
      })

      this.setData({
        trackList: this.data.trackList.concat(content.rows)
      })
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.defaultTabIndex == 0) return

    // console.log("加载更多数据。。。。");

    if (this.data.trackPage > 1 && this.data.trackPage >= this.data.trackPageMax) {
      // console.log("无更多数据。。。。");
      return
    }

    this.loadTrackData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

})